/*
 * Copyright (c) 2007 The University of Reading
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University of Reading, nor the names of the
 *    authors or contributors may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package uk.ac.rdg.resc.wms2kml;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.load.Commit;
import org.simpleframework.xml.load.PersistenceException;
import org.simpleframework.xml.load.Validate;

/**
 * Class representing the root of a version 1.1.1 Capabilities document.
 *
 * @author Jon Blower
 * $Revision$
 * $Date$
 * $Log$
 */
// Strict=false means that we ignore any tags and attributes that
// aren't listed in this class or any subclasses
@Root(name ="WMT_MS_Capabilities", strict=false)
public class WMS1_1_1Capabilities extends WMSCapabilities
{
    static final String PLATE_CARREE_PROJ_CODE = "EPSG:4326";
    
    @Attribute(name="version")
    private String version;
    
    @Element(name="Service")
    private Service1_1_1 service;
    
    @Element(name="Capability")
    private Capability1_1_1 capability;
    
    private String compatibleImageFormat = null;

    public String getVersion()
    {
        return version;
    }

    public String getServerDescription()
    {
        return this.service.abstr;
    }

    public String getServerTitle()
    {
        return this.service.title;
    }

    public Layer getRootLayer()
    {
        return this.capability.rootLayer;
    }
    
    public String getGetMapUrl()
    {
        String url = this.capability.request.getMap.dcpType.http.get.onlineResource.href;
        return url.endsWith("?") ? url : url + "?";
    }
    
    public String getLatLonProjectionCode()
    {
        return "SRS=" + PLATE_CARREE_PROJ_CODE;
    }

    @Validate
    private void checkCompatibleFormat() throws PersistenceException
    {
        String compatibleFormat = this.findCompatibleImageFormat(
            this.capability.request.getMap.supportedFormats);
        if (compatibleFormat == null)
        {
            throw new PersistenceException("This WMS does not support" +
                " any image format that Google Earth understands");
        }
        this.compatibleImageFormat = compatibleFormat;
    }

    public String getCompatibleImageFormat()
    {
        return this.compatibleImageFormat;
    }
}

@Root(name="Service", strict=false)
class Service1_1_1
{
    @Element(name="Title", required=false) // For some reason, title is not always provided
    String title = "(title not provided in Capabilities document)";
    
    @Element(name="Abstract", required=false)
    String abstr = "(abstract not provided in Capabilities document)"; // "abstract" is a reserved word in Java
    
    @Element(name="MaxWidth", required=false)
    int maxImageWidth = Integer.MAX_VALUE;
    
    @Element(name="MaxHeight", required=false)
    int maxImageHeight = Integer.MAX_VALUE;
}

@Root(name="Capability", strict=false)
class Capability1_1_1
{
    @Element(name="Request")
    Request request;
    
    @Element(name="Layer")
    Layer1_1_1 rootLayer;
}

